﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gasco.Business.Services;
using Gasco.DataAccess;
using Mani.Common;

public partial class Controls_platts_add : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadProducts();
            LoadCurrency();
            LoadPlattsType();
            LoadUnit();

            if (Request.QueryString["id"] != null)
                LoadPlatts();
        }
    }

    private void LoadProducts()
    {
        ProductService ps = new ProductService();
        ddProducts.DataSource = ps.GetProducts();
        ddProducts.DataBind();
    }

    private void LoadPlattsType()
    {
        LookupService<platts_type> plattsTypeService = new LookupService<platts_type>();
        ddPlattsType.DataSource = plattsTypeService.GetAll();
        ddPlattsType.DataBind();
    }

    private void LoadCurrency()
    {
        LookupService<currency> currencyService = new LookupService<currency>();
        ddCurrency.DataSource = currencyService.GetAll();
        ddCurrency.DataBind();
    }

    private void LoadUnit()
    {
        LookupService<unit> uService = new LookupService<unit>();
        ddUnit.DataSource = uService.GetAll();
        ddUnit.DataBind();
    }

    private void LoadPlatts()
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        PlattService ps = new PlattService();
        platt p = ps.GetById(id);
        ddProducts.SelectedValue = p.product_id.ToString();
        ddPlattsType.SelectedValue = p.platt_type_id.ToString();
        ddCurrency.SelectedValue = p.currency_id.ToString();
        ddUnit.SelectedValue = p.unit_id.ToString();
        tbPrice.Text = p.price.ToString();
        tbPlattDate.Text = p.create_date.ToJalali();
    }



    protected void btnclose_Click(object sender, EventArgs e)
    {
        InsertUpdateInfo();
    }

    private void InsertUpdateInfo()
    {
        bool InsertMode = (Request.QueryString["id"] == null);
        PlattService ps = new PlattService();
        platt p = null;

        if (InsertMode)
        {
            p = new platt();
        }
        else
        {
            p = ps.GetById(Convert.ToInt32(Request.QueryString["id"]));
        }

        p.currency_id = Convert.ToInt32(ddCurrency.SelectedValue);
        p.platt_type_id = Convert.ToInt32(ddPlattsType.SelectedValue);
        p.price = Convert.ToSingle(tbPrice.Text);
        p.product_id = Convert.ToInt32(ddProducts.SelectedValue);
        p.unit_id = Convert.ToInt32(ddUnit.SelectedValue);
        p.create_date = Convert.ToDateTime(tbPlattDate.Text);
        p.update_date = DateTime.Now;

        try
        {
            if (InsertMode)
                ps.Create(p);
            else
                ps.Update(p);
        }
        catch (DuplicatePlattException)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("DuplicatePlattException").ToString());
            return;
        }

        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script1", "<script>pageClose();</script>");
    }


}
